﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.Data.SqlClient;
using System.Data.Common;

//http://doc.ddart.net/mssql/sql70/sp_00.htm
namespace DbSetupCreator
{
    public partial class FormMain : Form
    {
        FormLogOn logOn = new FormLogOn();
        //DbCommand command;

        public FormMain()
        {
            InitializeComponent();
        }

        private void FormMain_Load(object sender, EventArgs e)
        {
            if (logOn.ShowDialog() == DialogResult.OK)
                PopulateForm();
            else
                this.Close();
        }

        private void PopulateForm()
        {
            //create needed objects.
            DbCommand command = new SqlCommand();
            DbDataAdapter da = new SqlDataAdapter();
            DataSet ds = new DataSet();

            //set up connection and open the connection
            command.Connection = logOn.Connection;

            command.CommandText = "sp_databases";
            command.CommandType = CommandType.StoredProcedure;

            da.SelectCommand = command;
            da.Fill(ds);


            DataTable dt = ds.Tables[0];

            foreach (DataRow dr in dt.Rows)
            {
                cmbDatabases.Items.Add(dr["DATABASE_NAME"]);
            }
        }

        private void cmbDatabases_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!btnWork.Enabled)
                btnWork.Enabled = true;

            //clear the old cbl
            cblTables.Items.Clear();

            //create needed objects.
            DbCommand command = new SqlCommand();
            DbDataAdapter da = new SqlDataAdapter();
            DataSet ds = new DataSet();

            //set up connection and open the connection
            logOn.Connection.ConnectionString = logOn.ConnectionString.Replace("master", cmbDatabases.SelectedItem.ToString());
            command.Connection = logOn.Connection;

            command.CommandText = "SP_Tables";
            command.CommandType = CommandType.StoredProcedure;

            da.SelectCommand = command;
            da.Fill(ds);

            DataTable dt = ds.Tables[0];

            foreach (DataRow dr in dt.Rows)
            {
                if(dr["TABLE_TYPE"].ToString() == "TABLE")
                    cblTables.Items.Add(dr["TABLE_NAME"]);
            }
        }

        private void btnSelectAll_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < cblTables.Items.Count; i++)
			{
			    cblTables.SetItemChecked(i, true);
			}
        }

        private void btnSelectNone_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < cblTables.Items.Count; i++)
            {
                cblTables.SetItemChecked(i, false);
            }
        }
    }
}
